/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ludowork.ludovisor;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.List;
import ludowork.components.Visor;
import ludowork.core.Manager;
import ludowork.core.PluginLoader;
import ludowork.tools.PluginsManager;

/**
 *
 * @author becario1
 */
public class Ludovisor extends javax.swing.JFrame {

    public Ludovisor() {
        visor = new Visor();
        manager = Manager.getManager();
        xInit = 0;
        yInit = 0;
        drag = false;
        fileName = "";
        zonesNumber = 0;
        pagesCount = 0;
        xtra = "";
        focusedZone = -1;
        imageSourcesManager = new PluginsManager();
        initComponents();
        extraInit();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Ludovisor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Ludovisor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Ludovisor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Ludovisor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Ludovisor().setVisible(true);
            }
        });
    }

    public void setMsg(String msg) {
        xtra = msg;
    }

    public Rectangle getFocusedZone() {
        if (zonesFound != null && (focusedZone >= 0) && (focusedZone < zonesFound.size())) {
            return zonesFound.get(focusedZone);
        }
        return null;
    }

    private void extraInit() {
        visorMenuBar.generateFileMenu(imageSourcesManager.getImageSources());
        pageNumberText.setText("" + 0);
        manager.setImageStore(visor);
    }

    private void setPageNumber() {
        if (manager.getImageSource() == null) {
            pageNumberText.setText("" + 0);
            manager.getLogger().addLine("No image source");
        }
        pageNumberText.setText("" + manager.getImageStore().getActiveImageId());
    }

    /**
     * build status bar msg
     */
    private void builStatusBar() {
        String coloModel = "";
        String coordinates = "";
        if (overImage) {
            coordinates = "" + xCoord + "x" + yCoord;
        }

        //contruir DE VERDAD la cadena

        int page = 0;
        if (manager.getImageSource() != null) {
            page = manager.getImageStore().getActiveImageId();
        }

        statusBar.setText("Document: " + fileName + " | Page: " + page + "-" + pagesCount + " | " + coloModel + " | Zones found: " + zonesNumber + " | " + coordinates + " | " + xtra);
    }

    private void goSpecificPage() throws NumberFormatException {
        try {
            visor.setActiveImage(Integer.parseInt(pageNumberText.getText()));
        } catch (NumberFormatException exception) {
            manager.getLogger().addLine("incorrect page number");
        }
        setPageNumber();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        controlPanel = new javax.swing.JPanel();
        pageNumberText = new javax.swing.JTextField();
        prevButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        statusBar = new javax.swing.JLabel();
        visor = new ludowork.components.Visor();
        visorMenuBar = new ludowork.components.VisorMenuBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ludovisor");
        setMinimumSize(new java.awt.Dimension(350, 450));

        controlPanel.setAlignmentX(0.0F);
        controlPanel.setAlignmentY(0.0F);
        controlPanel.setMinimumSize(new java.awt.Dimension(120, 300));

        pageNumberText.setFont(new java.awt.Font("Arial", 0, 10)); // NOI18N
        pageNumberText.setText("0");
        pageNumberText.setAlignmentX(0.0F);
        pageNumberText.setAlignmentY(0.0F);
        pageNumberText.setMargin(new java.awt.Insets(0, 0, 0, 0));
        pageNumberText.setMinimumSize(new java.awt.Dimension(2, 14));
        pageNumberText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pageNumberTextActionPerformed(evt);
            }
        });
        pageNumberText.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                pageNumberTextFocusLost(evt);
            }
        });

        prevButton.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        prevButton.setText("<");
        prevButton.setToolTipText("prev page");
        prevButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        prevButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                prevClicked(evt);
            }
        });

        nextButton.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        nextButton.setText(">");
        nextButton.setToolTipText("next page");
        nextButton.setIconTextGap(0);
        nextButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        nextButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nextClicked(evt);
            }
        });

        statusBar.setText("No Regions Found");
        statusBar.setAlignmentY(0.0F);
        statusBar.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        statusBar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        javax.swing.GroupLayout controlPanelLayout = new javax.swing.GroupLayout(controlPanel);
        controlPanel.setLayout(controlPanelLayout);
        controlPanelLayout.setHorizontalGroup(
            controlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(controlPanelLayout.createSequentialGroup()
                .addComponent(prevButton, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(pageNumberText, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(nextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(statusBar, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE))
        );
        controlPanelLayout.setVerticalGroup(
            controlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(controlPanelLayout.createSequentialGroup()
                .addGroup(controlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(nextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(prevButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pageNumberText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(statusBar))
                .addGap(0, 0, 0))
        );

        controlPanelLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {nextButton, pageNumberText, prevButton});

        visor.setBackground(new java.awt.Color(102, 102, 102));
        visor.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                mouseScrollOnViewer(evt);
            }
        });
        visor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                visorMouseReleased(evt);
            }
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                visorMouseClick(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                visorMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                visorMouseExited(evt);
            }
        });
        visor.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                visorMouseDrag(evt);
            }
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                visorMouseMoved(evt);
            }
        });

        javax.swing.GroupLayout visorLayout = new javax.swing.GroupLayout(visor);
        visor.setLayout(visorLayout);
        visorLayout.setHorizontalGroup(
            visorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        visorLayout.setVerticalGroup(
            visorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 207, Short.MAX_VALUE)
        );

        setJMenuBar(visorMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(controlPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(visor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(controlPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(visor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mouseScrollOnViewer(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_mouseScrollOnViewer
        int notches = evt.getWheelRotation();
        if (notches < 0) {
            visor.incrementRatio(evt.getX(), evt.getY());
        } else {
            visor.decrementRatio(evt.getX(), evt.getY());
        }
        //statusBar.setText("Zoom pos: " + evt.getX() + "x" + evt.getY());
        repaint();
    }//GEN-LAST:event_mouseScrollOnViewer

    private void visorMouseClick(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_visorMouseClick
    }//GEN-LAST:event_visorMouseClick

    private void visorMouseDrag(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_visorMouseDrag
        if (drag) {
            visor.shiftOrigin(evt.getX() - xInit, evt.getY() - yInit);
            repaint();
            //statusBar.setText("Drag: " + (evt.getX() - xInit) + "x" + (evt.getY() - yInit));
        } else {
            xInit = evt.getX();
            yInit = evt.getY();
            visor.fixOrigin();
            drag = true;
        }

    }//GEN-LAST:event_visorMouseDrag

    private void visorMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_visorMouseReleased
        visor.fixOrigin();
        repaint();
        //statusBar.setText("Rel: " + evt.getX() + "x" + evt.getX());
        drag = false;
    }//GEN-LAST:event_visorMouseReleased

    private void visorMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_visorMouseEntered
        overImage = true;
    }//GEN-LAST:event_visorMouseEntered

    private void visorMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_visorMouseExited
        overImage = false;
    }//GEN-LAST:event_visorMouseExited

    private void visorMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_visorMouseMoved
        //statusBar.setText(visor2.absoluteX(evt.getX()) + "x" + visor2.absoluteY(evt.getY()));
        xCoord = visor.absoluteX(evt.getX());
        yCoord = visor.absoluteY(evt.getY());
        builStatusBar();
        repaint();
    }//GEN-LAST:event_visorMouseMoved

    private void nextClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nextClicked
        visor.nextImage();
        setPageNumber();
        repaint();
    }//GEN-LAST:event_nextClicked

    private void prevClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_prevClicked
        visor.prevImage();
        setPageNumber();
        repaint();
    }//GEN-LAST:event_prevClicked

    private void pageNumberTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_pageNumberTextFocusLost
        goSpecificPage();
        repaint();
    }//GEN-LAST:event_pageNumberTextFocusLost

    private void pageNumberTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pageNumberTextActionPerformed
        goSpecificPage();
        repaint();
    }//GEN-LAST:event_pageNumberTextActionPerformed
    //Status bar
    private int xCoord;
    private int yCoord;
    private boolean overImage;
    private String fileName;
    private int zonesNumber;
    private int pagesCount;
    private String xtra;
    //Non automatic variables.
    //private BufferedImage inputImg;
    private int xInit;
    private int yInit;
    private boolean drag;
    private int focusedZone;
    private List<Rectangle> zonesFound;
    private Manager manager;
    private PluginsManager imageSourcesManager;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel controlPanel;
    private javax.swing.JButton nextButton;
    private javax.swing.JTextField pageNumberText;
    private javax.swing.JButton prevButton;
    private javax.swing.JLabel statusBar;
    private ludowork.components.Visor visor;
    private ludowork.components.VisorMenuBar visorMenuBar;
    // End of variables declaration//GEN-END:variables
}
//asocia bien los sliders a su cosas. gañan. que eres un gañan

